package com.mdd.front.service.leasing.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mdd.front.service.leasing.ILeasingSchemeDetailsFrontService;
import com.mdd.front.vo.leasing.LeasingSchemeDetailsVo;
import com.mdd.product.entity.LeasingSchemeDetails;
import com.mdd.product.mapper.LeasingSchemeDetailsMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

/**
 * 租赁方案详细实现类
 * @author LikeAdmin
 */
@Service
public class LeasingSchemeDetailsFrontServiceImpl implements ILeasingSchemeDetailsFrontService {

    @Resource
    LeasingSchemeDetailsMapper leasingSchemeDetailsMapper;

    /**
     * 租赁方案详细列表
     *
     * @author LikeAdmin
     * @param spuId 商品id
     * @return PageResult<OmsLeasingSchemeDetilsListedVo>
     */
    @Override
    public List<LeasingSchemeDetailsVo> list(Long spuId,Double riskScore) {
        LambdaQueryWrapper<LeasingSchemeDetails> leasingSchemeDetailsQueryWrapper=new LambdaQueryWrapper<>();
        leasingSchemeDetailsQueryWrapper.eq(LeasingSchemeDetails::getSpuId,spuId);
        List<LeasingSchemeDetails> leasingSchemeDetails = leasingSchemeDetailsMapper.selectList(leasingSchemeDetailsQueryWrapper);

        List<LeasingSchemeDetailsVo> list = new LinkedList<>();
        for(LeasingSchemeDetails item : leasingSchemeDetails) {
            LeasingSchemeDetailsVo vo = new LeasingSchemeDetailsVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
            //0-10免押金，10-30，押金50%，30以上100%押金
            if(Objects.isNull(riskScore) || riskScore >=30){
                vo.setActualDeposit(vo.getDeposit());
                vo.setExemptDeposit(new BigDecimal(0));
            }else if(riskScore>=10 && riskScore<30){
                vo.setActualDeposit(vo.getDeposit().multiply(new BigDecimal(0.5)));
                vo.setExemptDeposit(vo.getDeposit().multiply(new BigDecimal(0.5)));
            }else if(riskScore>=0 && riskScore<10){
                vo.setActualDeposit(vo.getDeposit());
                vo.setExemptDeposit(vo.getDeposit());
            }
        }

        return list;
    }

    /**
     * 租赁方案详细详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return OmsLeasingSchemeDetils
     */
    @Override
    public LeasingSchemeDetailsVo detail(Long id) {
        LeasingSchemeDetails model = leasingSchemeDetailsMapper.selectOne(
                new QueryWrapper<LeasingSchemeDetails>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        LeasingSchemeDetailsVo vo = new LeasingSchemeDetailsVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    @Override
    public LeasingSchemeDetailsVo getFirst(Long spuId) {
        LeasingSchemeDetails leasingSchemeDetails = leasingSchemeDetailsMapper.selectOne(new LambdaQueryWrapper<LeasingSchemeDetails>().eq(LeasingSchemeDetails:: getSpuId,spuId).last(" limit 1"));
        LeasingSchemeDetailsVo leasingSchemeDetailsVo = new LeasingSchemeDetailsVo();
        if(Objects.nonNull(leasingSchemeDetails)) {
            BeanUtils.copyProperties(leasingSchemeDetails, leasingSchemeDetailsVo);
        }
        return leasingSchemeDetailsVo;
    }
}
